package com.zjut.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjut.dao.ChapterDao;
import com.zjut.domain.Chapter;
import com.zjut.domain.Paper;
import com.zjut.domain.Chapter;
import com.zjut.service.impl.ChapterServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChapterService implements ChapterServiceImpl {
    @Autowired
    private ChapterDao chapterDao;

    @Override
    public boolean save(Chapter chapter) {
        return chapterDao.insert(chapter) > 0;
    }

    @Override
    public boolean update(Chapter chapter) {
        return chapterDao.updateById(chapter) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return chapterDao.deleteById(id) > 0;
    }

    //通过ID查找
    @Override
    public Chapter getById(Integer id) {
        return chapterDao.selectById(id);
    }

    //查找全部
    @Override
    public List<Chapter> getAll() {
        return chapterDao.selectList(null);
    }

    @Override
    public List<Chapter> getChapter(Paper paper) {
        LambdaQueryWrapper<Chapter> lqw = new LambdaQueryWrapper<>();
        if (!paper.getMinChapter().equals(paper.getMaxChapter())){
            lqw.gt(Chapter::getChapter, paper.getMinChapter())
                    .lt(Chapter::getChapter, paper.getMaxChapter())
                    .or(q -> q.eq(Chapter::getChapter, paper.getMinChapter())
                            .ge(Chapter::getSection, paper.getMinSection()))
                    .or(q -> q.eq(Chapter::getChapter, paper.getMaxChapter())
                            .le(Chapter::getSection, paper.getMaxSection()));
        }else if(paper.getMinChapter().equals(paper.getMaxChapter())){
            lqw.eq(Chapter::getChapter, paper.getMaxChapter())
                    .le(Chapter::getSection,paper.getMaxSection())
                    .ge(Chapter::getSection,paper.getMinSection());
        }
        return chapterDao.selectList(lqw);

    }
    @Override
    public char getRequirement(int chapter, int section) {
        LambdaQueryWrapper<Chapter> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Chapter::getChapter, chapter).eq(Chapter::getSection, section);
        return chapterDao.selectOne(lqw).getRequirement();
    }
    @Override
    public String getKnowledge(int chapter, int section) {
        LambdaQueryWrapper<Chapter> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Chapter::getChapter, chapter).eq(Chapter::getSection, section);
        return chapterDao.selectOne(lqw).getKnowledge();
    }


}
